<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/10/30 0030
 * Time: 16:37
 */
namespace plugins\qbt_wx_os\model;//Demo插件英文名，改成你的插件英文就行了
use think\Model;

class WxOsReplyModel extends Model{
    protected $name = 'wx_os_reply';

    public function data_info($where){
        return $this->where($where)->find();
    }

    public function data_list($where){
        return $this->where($where)->select()->toArray();
    }

    public function update_data($where,$data){
        return $this->where($where)->update($data);
    }

    public function add_data($data){
        return $this->insertGetId($data);
    }

    public function update_action($where,$data){
        $info = $this->data_info($where);
        if($info['type'] == 2 && $data['type'] == 2){
            $info['img_url'] = json_decode($info['image'],true)['img_url'];
            $data['img_url'] = json_decode($data['image'],true)['img_url'];
            if($info['img_url'] != $data['img_url']){
               unlink('../public/upload/'.$info['img_url']);
            }
            unset($data['img_url']);
        }else if($info['type']  == 2){
            $info['img_url'] = json_decode($info['image'],true)['img_url'];
            unlink('../public/upload/'.$info['img_url']);
        }
        return $this->update_data($where,$data);
    }

    public function del_data($where){
        $info = $this->data_info($where);
        if($info['type'] == 2){
            $info['img_url'] = json_decode($info['image'],true)['img_url'];
            unlink('../public/upload/'.$info['img_url']);
        }
        return $this->where($where)->delete();
    }
}